package com.jmult.crop;

import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.jfinal.upload.UploadFile;

public class CropperController extends Controller {

	@Inject
	CropperService srv;

	public void index() {
		int pageNumber = getInt(0, 1);
		setAttr("images", srv.getUploadImages(pageNumber));
		render("index.html");
	}

	public void upload() {
		UploadFile uf = null;
		try {
			uf = getFile("file", "/", srv.getImageMaxSize());
			if (uf == null) {
				renderJson(Ret.fail("msg", "请先选择图片"));
				return;
			}
			
			renderJson(srv.cropImage(getPara("putData"), uf));
			
		} catch (Exception e) {
			if (e instanceof com.jfinal.upload.ExceededSizeException) {
				renderJson(Ret.fail("msg", "Wo~~ 这图也太大了吧！！别超过 8M 啊"));
			} else {
				if (uf != null) {
					uf.getFile().delete();
				}
				renderJson(Ret.fail("msg", e.getMessage()));
			}
			return ;
		}		
		
	}
}
